package com.zhuxiang.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务器端
 * 编程实现服务器的初始化和关闭
 */
public class Server {
    /**
     * 自定义成员变量
     */
    private ServerSocket ss;
    private Socket s;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;

    public ObjectInputStream getOis() {
        return ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }
    /**
     * 自定义成员方法实现服务器的初始化
     */
    public void serverInit() throws IOException {
            ss = new ServerSocket(9999);
            System.out.println("等待客户端的连接。。。");
            s = ss.accept();
            System.out.println("客户端连接成功");
            ois = new ObjectInputStream(s.getInputStream());
            oos = new ObjectOutputStream(s.getOutputStream());
            System.out.println("服务器初始化成功");
    }

    /**
     * 自定义成员方法实现服务器的关闭
     */
    public void serverClose() throws IOException {
        oos.close();
        ois.close();
        s.close();
        ss.close();
        System.out.println("服务器成功关闭");
    }
}
